package com.xiaoshuidi.cloud.module.finance.service.taxRate.impl;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.contract.vo.ContractInvoiceDetailVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceTitleConfigReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceTitleConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.taxRate.vo.FinanceTaxRateConfigReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.taxRate.vo.FinanceTaxRateConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.taxRate.vo.InvoiceTaxRateConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.taxRate.vo.TaxRateConfigVO;
import com.xiaoshuidi.cloud.module.finance.convert.invoice.FinanceInvoiceTitleConfigConvert;
import com.xiaoshuidi.cloud.module.finance.convert.taxRate.FinanceTaxRateConfigConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceTitleConfigDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.taxRate.FinanceTaxRateConfigDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.FinanceInvoiceTitleConfigMapper;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.taxRate.FinanceTaxRateConfigMapper;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceTitleConfigService;
import com.xiaoshuidi.cloud.module.finance.service.taxRate.FinanceTaxRateConfigService;
import com.xiaoshuidi.cloud.module.system.api.apartment.ApartmentManageApi;
import com.xiaoshuidi.cloud.module.system.api.apartment.dto.TaxRateConfigDTO;
import com.xiaoshuidi.cloud.module.system.enums.apartment.ApartmentManageEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class FinanceTaxRateConfigServiceImpl implements FinanceTaxRateConfigService {

    @Resource
    private FinanceTaxRateConfigMapper financeTaxRateConfigMapper;
    @Resource
    private ApartmentManageApi apartmentManageApi;
    /**
     * 获取配置信息
     *
     * @param apartmentId
     * @return
     */
    @Override
    public InvoiceTaxRateConfigRespVO get(Long apartmentId) {
        CommonResult<List<TaxRateConfigDTO>> result = apartmentManageApi.getConfigFeesList(apartmentId);
        List<TaxRateConfigDTO> list = result.getData();
        if(null==list||list.size()<1){
            return InvoiceTaxRateConfigRespVO.builder().build();
        }
//        List<FinanceTaxRateConfigRespVO> list = financeTaxRateConfigMapper.getFinanceTaxRateConfig(apartmentId);
//        if(null==list||list.size()<1){
//            return InvoiceTaxRateConfigRespVO.builder().build();
//        }
        return InvoiceTaxRateConfigRespVO.builder()
                .apartmentId(list.get(0).getApartmentId())
                .apartmentName(list.get(0).getApartmentName())
                .isSimpleTaxation(list.get(0).isSimpleTaxation())
                .expenseType(list.get(0).getExpenseType())
                .taxRateConfigList(list)
                .build();
    }

    /**
     * 保存配置信息
     *
     * @param financeTaxRateConfigReqVO
     */
    @Override
    public void save(FinanceTaxRateConfigReqVO financeTaxRateConfigReqVO) {
        List<FinanceTaxRateConfigDO> taxRateConfigDOList = financeTaxRateConfigMapper.selectList(FinanceTaxRateConfigDO::getApartmentId, financeTaxRateConfigReqVO.getApartmentId());
        if(null==taxRateConfigDOList||taxRateConfigDOList.size()<1){
            for(TaxRateConfigVO taxRateConfigVO : financeTaxRateConfigReqVO.getTaxRateConfigList()){
                financeTaxRateConfigMapper.insert(
                        FinanceTaxRateConfigDO.builder()
                                .taxRate(taxRateConfigVO.getTaxRate())
                                .configFeesName(taxRateConfigVO.getConfigFeesName())
                                .apartmentId(financeTaxRateConfigReqVO.getApartmentId())
                                .apartmentName(financeTaxRateConfigReqVO.getApartmentName())
                                .isSimpleTaxation(financeTaxRateConfigReqVO.isSimpleTaxation())
                                .expenseType(financeTaxRateConfigReqVO.getExpenseType())
                                .build());
            }
            apartmentManageApi.updateApartment(financeTaxRateConfigReqVO.getApartmentId(), ApartmentManageEnum.TAX_RATE_CONFIG.getCode());
        }else{
            for(TaxRateConfigVO taxRateConfigVO : financeTaxRateConfigReqVO.getTaxRateConfigList()){
                for(int i=0;i<=taxRateConfigDOList.size()-1;i++){
                    if(StringUtils.equals(taxRateConfigVO.getConfigFeesName(),taxRateConfigDOList.get(i).getConfigFeesName())){
                        taxRateConfigDOList.get(i).setTaxRate(taxRateConfigVO.getTaxRate());
                        financeTaxRateConfigMapper.updateById(taxRateConfigDOList.get(i));
                        break;
                    }
                    if(i==taxRateConfigDOList.size()-1){
                        financeTaxRateConfigMapper.insert(
                                FinanceTaxRateConfigDO.builder()
                                        .taxRate(taxRateConfigVO.getTaxRate())
                                        .configFeesName(taxRateConfigVO.getConfigFeesName())
                                        .apartmentId(financeTaxRateConfigReqVO.getApartmentId())
                                        .apartmentName(financeTaxRateConfigReqVO.getApartmentName())
                                        .isSimpleTaxation(financeTaxRateConfigReqVO.isSimpleTaxation())
                                        .expenseType(financeTaxRateConfigReqVO.getExpenseType())
                                        .build());
                    }
                }
            }
        }
    }


}
